import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STColumn, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { ShipperBaseService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { RebateManagementService } from '../../services/rebate-management.service';
import { ParterRebateManageMenAbnormalFeedbackComponent } from '../../model/abnormal-feedback/abnormal-feedback.component';

@Component({
  selector: 'app-parter-channel-rebate-management-setting',
  templateUrl: './rebate-setting.component.html'
})
export class ParterRebateManageMentSettingComponent implements OnInit {
  schema: SFSchema = {};
  columns!: STColumn[];
  ui!: SFUISchema;
  @ViewChild('st', { static: false })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  spuStatus = '1';
  _$expand = false;
  constructor(
    public router: Router,
    public service: RebateManagementService,
    private modal: NzModalService,
    public shipperservice: ShipperBaseService
  ) {}
  /**
   * 查询参数
   */
  get reqParams() {
    const params: any = Object.assign({}, this.sf?.value || {});
    return {
      ...params
    };
  }
  ngOnInit() {
    this.initSF();
    this.initST();
  }

  initSF() {
    this.schema = {
      properties: {
        configName: {
          type: 'string',
          title: '模板名称'
        },
        stateLocked: {
          type: 'string',
          title: '状态',
          enum: [
            { label: '全部', value: '' },
            { label: '生效中', value: 0 },
            { label: '失效', value: 1 }
          ],
          ui: {
            widget: 'select'
          }
        }
      }
    };
    this.ui = {
      '*': {
        spanLabelFixed: 140,
        grid: { span: 8, gutter: 4 }
      }
    };
  }

  initST() {
    this.columns = [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return (this.st.pi * this.st.ps) - this.st.ps + idx + 1 + ''
        }
      },
      {
        title: '模板名称',
        index: 'configName',
        width: '200px'
      },
      {
        title: '等级类型',
        render: 'configType',
        width: '200px'
      },
      {
        title: '备注',
        index: 'remark',
        width: '250px'
      },
      {
        title: '合伙人范围',
        render: 'partnerType',
        width: '200px'
      },
      {
        title: '创建时间',
        index: 'createTime',
        width: '200px'
      },
      {
        title: '生效时间',
        index: 'enableTime',
        width: '200px'
      },
      {
        title: '优先级',
        index: 'priority',
        width: '100px'
      },
      {
        title: '状态',
        render: 'stateLocked',
        width: '100px'
      },
      {
        title: '操作',
        fixed: 'right',
        width: '120px',
        className: 'text-left',
        buttons: [
          {
            text: '查看',
            acl: { ability: ['REBATE-SETTING-detail'] },
            click: _record => this.configAction(_record)
          },
          {
            text: '禁用',
            acl: { ability: ['REBATE-SETTING-forbidden'] },
            iif: _record => {
              return _record.stateLocked == false && (_record.partnerType == 3 || _record.partnerType == 2);
            },
            click: _record => this.viewEvaluate(_record)
          },
          {
            text: '启用',
            acl: { ability: ['REBATE-SETTING-startUseing'] },
            iif: _record => {
              return _record.stateLocked == true;
            },
            click: _record => this.viewEvaluate(_record)
          }
        ]
      }
    ];
  }
  /**
   *禁用
   */
  viewEvaluate(item: any) {
    console.log(item.stateLocked);
    let title = '';
    let stateLocked: boolean;
    if (item.stateLocked) {
      title = '是否禁用该配置？';
      stateLocked = false;
    } else {
      title = '是否启用该配置？';
      stateLocked = true;
    }
    this.modal.confirm({
      nzTitle: title,
      nzOnOk: () => {
        const params = {
          id: item?.id,
          stateLocked: stateLocked
        };
        this.service.request(this.service.$api_set_updateRebateConfig, params).subscribe((res: any) => {
          if (res) {
            this.service.msgSrv.success('设置成功！');
            this.st.reload();
          }
        });
      }
    });
  }
  /**
   *查看
   */
  feedback(item?: any) {
    const modal = this.modal.create({
      nzTitle: '查看',
      nzWidth: 580,
      nzContent: ParterRebateManageMenAbnormalFeedbackComponent,
      nzComponentParams: { i: item },
      nzFooter: null
    });
    modal.afterClose.subscribe((res: any) => {
      if (res) {
      }
    });
  }
  configAction(value?: any) {
    this.router.navigate(['/partner/rebate/setting/add/', '0'], { queryParams: value });
  }
  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this.st.load(1);
  }
}
